package com.ruoyi.xm.service.impl;

import java.security.Security;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xm.mapper.ProjectMapper;
import com.ruoyi.xm.domain.Project;
import com.ruoyi.xm.service.IProjectService;

/**
 * 项目Service业务层处理
 * 
 * @author Me
 * @date 2025-05-17
 */
@Service
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目
     * 
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public Project selectProjectById(Long id)
    {
        return projectMapper.selectProjectById(id);
    }

    /**
     * 查询项目列表
     * 
     * @param project 项目
     * @return 项目
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目
     * 
     * @param project 项目
     * @return 结果
     */
    @Override
    public int insertProject(Project project)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        project.setCreateBy(String.valueOf(userId));
        project.setCreateTime(DateUtils.getNowDate());
        Project project1 = new Project();
        project1.setName(project.getName());
        List<Project> projects = projectMapper.selectProjectList(project1);
        if(!projects.isEmpty()){
            throw new SecurityException("项目名称重复");
        }
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目
     * 
     * @param project 项目
     * @return 结果
     */
    @Override
    public int updateProject(Project project)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        project.setUpdateBy(String.valueOf(userId));
        project.setUpdateTime(DateUtils.getNowDate());
        if(3==project.getState()&&project.getToBeCompletedTask()!=0){
            throw new SecurityException("还有个"+project.getToBeCompletedTask()+"任务未完成,请先完成项目相关任务");
        }
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目
     * 
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByIds(Long[] ids)
    {
        return projectMapper.deleteProjectByIds(ids);
    }

    /**
     * 删除项目信息
     * 
     * @param id 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id)
    {
        return projectMapper.deleteProjectById(id);
    }
}
